﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MyVoteMVC.BackstageManagement.Controllers;
using MyVoteMVC.Domain;


namespace MyVoteMVC.BackstageManagement.Views
{
    public partial class ImportProjectToItem : Form,IItemView
    {
        public event EventHandler OnAddComplete;

        private string questionId;

        public IItemController controller;

        public ImportProjectToItem(string questionId)
        {
            InitializeComponent();
            controller = new ItemController(this);
            this.questionId = questionId;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void AddItem_FormClosing(object sender, FormClosingEventArgs e)
        {
            OnAddComplete(null, null);
        }

        public void CreateItemComplete(string message)
        {
            MessageBox.Show(message);
        }

        public void EditItemComplete(string message)
        {
            throw new NotImplementedException();
        }

        public void GetItemComplete(VT_Items item)
        {
            throw new NotImplementedException();
        }


        private void btnSave_Click(object sender, EventArgs e)
        {
            if (gvProject.SelectedRows.Count > 0)
            {
                List<VT_Items> itemsToAdd = new List<VT_Items>();
                foreach (DataGridViewRow row in gvProject.SelectedRows)
                {
                    VT_Projects project = row.DataBoundItem as VT_Projects;
                    VT_Items newItem = new VT_Items();

                    newItem.Question_ID = questionId;
                    newItem.Item_Title = project.Project_Name;
                    newItem.Item_Content = project.Project_Description;
                    newItem.Item_IfDefault = 0;
                    newItem.Project_ID = project.Project_ID;
                    newItem.ItemImage = new VT_ItemImage()
                    {
                        Item_Image = ((project.ProjectImage != null && project.ProjectImage.Project_Image != null) ? project.ProjectImage.Project_Image : null)
                    };
                    itemsToAdd.Add(newItem);
                }
                controller.CreateItems(itemsToAdd);
            }
            else
            {
            }
        }

        public void GetProjectsComplete(IList<VT_Projects> projects)
        {
            gvProject.DataSource = projects;
        }

        private void AddItem_Load(object sender, EventArgs e)
        {
            controller.GetProjects();
        }

        public void OnSubmitError(string message)
        {
            MessageBox.Show(message);
        }

        private void btnBrowser_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrWhiteSpace(txtKeyWord.Text))
            {
                string kw = txtKeyWord.Text;

                controller.GetProjects(kw);
            } 
        }

        private void btnSelectAll_Click(object sender, EventArgs e)
        {
            gvProject.SelectAll();
        }


        public void CreateItemsComplete(string message)
        {
            MessageBox.Show(message);
        }

        public void GetItemsComplete(IList<VT_Items> item)
        {
            gvProject.DataSource = item;
        }
    }
}
